package down.xiaoheihe.work.Service;

import down.xiaoheihe.work.Entity.*;
import down.xiaoheihe.work.Mapper.TOrderMapper;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.Response.orderR;
import down.xiaoheihe.work.Response.orderT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class service3 {
    @Autowired
    TimagesMapper imagesMapper;
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    UserMapper tUserMapper;
    public List<orderR> 查询我的所有订单 (Integer id) {
        List<orderR> result = new ArrayList<>();
        TOrderExample tOrderExample = new TOrderExample();
        tOrderExample.createCriteria().andUseruidEqualTo(id);
        List<TOrder> orders = tOrderMapper.selectByExample(tOrderExample);
        for (TOrder tproduct : orders) {
            orderR nR= new orderR();
            nR.setId(tproduct.getId());
            Integer productId = tproduct.getProductId();
            TProduct tProduct1 = tProductMapper.selectByPrimaryKey(productId);
            nR.setDescription(tProduct1.getDescription());
            nR.setTitle(tProduct1.getTitle());
            nR.setPrice(tProduct1.getPrice());
            TimagesExample timagesExample = new TimagesExample();
            timagesExample.createCriteria().andProductIdEqualTo(productId);
            List<Timages> timages = imagesMapper.selectByExample(timagesExample);
            Timages timages1 = timages.get(0);
            nR.setUrl(timages1.getUrl());
            result.add(nR);
        }
        return result;
    }
    public List<orderR> 查询我卖的订单 (Integer id) {
        List<orderR> result = new ArrayList<>();
        TOrderExample tOrderExample = new TOrderExample();
        tOrderExample.createCriteria().andMaijiauidEqualTo(id);
        List<TOrder> orders = tOrderMapper.selectByExample(tOrderExample);
        for (TOrder tproduct : orders) {
            orderR nR= new orderR();
            nR.setId(tproduct.getId());
            Integer productId = tproduct.getProductId();
            TProduct tProduct1 = tProductMapper.selectByPrimaryKey(productId);
            nR.setDescription(tProduct1.getDescription());
            nR.setTitle(tProduct1.getTitle());
            nR.setPrice(tProduct1.getPrice());
            TimagesExample timagesExample = new TimagesExample();
            timagesExample.createCriteria().andProductIdEqualTo(productId);
            List<Timages> timages = imagesMapper.selectByExample(timagesExample);
            Timages timages1 = timages.get(0);
            nR.setUrl(timages1.getUrl());
            result.add(nR);
        }
        return result;
    }

    public orderT 查询指定订单 (Integer id) {
        List<String> urls = new ArrayList<>();
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        TProduct tProduct1 = tProductMapper.selectByPrimaryKey(tOrder.getProductId());
        TimagesExample timagesExample = new TimagesExample();
        timagesExample.createCriteria().andProductIdEqualTo(tProduct1.getId());
        for (Timages timages : imagesMapper.selectByExample(timagesExample)) {
            urls.add(timages.getUrl());
        }
        orderT orderT = new orderT();
        orderT.setId(id);
        orderT.setGuarentee(tProduct1.getGuarentee());
        orderT.setPrice(tProduct1.getPrice());
        orderT.setDescription(tProduct1.getDescription());
        orderT.setUrls(urls);
        orderT.setStatus(tOrder.getStatus());
        orderT.setTotalPrice(tOrder.getTotalPrice());
        orderT.setDays(tOrder.getDays());
        orderT.setJihuiaddress(tOrder.getJihuiaddress());
        orderT.setFahuoaddress(tOrder.getFahuoaddress());
        orderT.setTitle(tProduct1.getTitle());
        return orderT;
    }
}
